package com.apartment.sevice.impl;

import com.apartment.dao.ApartmentDao;
import com.apartment.data.entity.Apartment;
import com.apartment.sevice.ApartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Author : xuzhiyong
 * Date : 2022/6/2 15:36
 */
@Service
public class ApartmentServiceImpl implements ApartmentService {
    @Autowired
    private ApartmentDao apartmentDao;

    @Override
    public List<Apartment> findAllApartment() {
        return apartmentDao.findAll();
    }

    @Override
    public boolean addApartment(Apartment apartment) {
        boolean flag = false;
        try {
            apartmentDao.save(apartment);
            flag = true;
        }catch (Exception e){
            System.out.println("添加失败");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Apartment findApartmentById(Integer id) {
        try {
            Optional<Apartment> apartment = apartmentDao.findById(id);
            if (apartment.isPresent()){
                return apartment.get();
            }
        }catch (Exception e){
            System.out.println("查看详情页失败！！！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Apartment> findApartmentByLandlordId(String id) {
        return apartmentDao.findApartmentByLandlordId(id);
    }
}
